<template>
  <div class="login-container">
    <!-- 左侧区域 -->
    <div class="login-left">
      <!-- logo -->
      <div class="logo">
        <div class="logo-square">
          <span class="logo-letter">L</span>
        </div>
        <span class="logo-text">LIANDO</span>
      </div>
      
      <!-- 标语 -->
      <div class="slogan">
        <div class="slogan-line">正</div>
        <div class="slogan-line">念</div>
        <div class="slogan-line">·</div>
        <div class="slogan-line">利</div>
        <div class="slogan-line">他</div>
        <div class="slogan-line slogan-space"></div>
        <div class="slogan-line">极</div>
        <div class="slogan-line">致</div>
        <div class="slogan-line">·</div>
        <div class="slogan-line">求</div>
        <div class="slogan-line">变</div>
      </div>
      
      <!-- 底部装饰 -->
      <div class="cloud-decoration"></div>
    </div>
    
    <!-- 右侧区域 -->
    <div class="login-right">
      <div class="login-form">
        <h2>账号登录</h2>
        <el-form :model="loginForm" :rules="rules" ref="loginForm" @submit.native.prevent="handleLogin">
          <el-form-item prop="username">
            <el-input 
              v-model="loginForm.username" 
              prefix-icon="el-icon-user"
              placeholder="请输入您的OA登录账号">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="请输入密码">
            </el-input>
          </el-form-item>
          
          <div class="forgot-password">
            <a href="#">忘记密码？</a>
          </div>
          
          <el-form-item>
            <el-button 
              type="primary" 
              class="login-btn" 
              :loading="isLoading"
              @click="handleLogin">
              {{ isLoading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
          
          <div class="browser-notice">
            <i class="el-icon-warning-outline"></i>
            <span>请您使用非IE浏览器访问，推荐：谷歌、Edge、火狐、360、QQ等浏览器 <a href="#" class="download-link">下载地址</a></span>
          </div>
        </el-form>
      </div>
      
      <!-- 页脚 -->
      <div class="login-footer">
        <p>Copyright © 2023 联东集团 ALL RIGHTS RESERVED. 京ICP备 xxxxx号-1 京公网安备xxxxxxxx号</p>
      </div>
    </div>
  </div>
</template>

<script>
import api from '@/api'
import { Message, Loading } from 'element-ui'

export default {
  name: 'LoginView',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        remember: false
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      isLoading: false,
      errorMessage: ''
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (!valid) {
          return false;
        }
        
        this.isLoading = true;
        
        // 调用登录API
        api.user.login({
          username: this.loginForm.username,
          password: this.loginForm.password
        })
        .then(response => {
          console.log(response,'登录响应成功');
        
          // 登录成功处理
          // 保存token信息和用户信息
          localStorage.setItem('access_token', response.access_token);
          localStorage.setItem('token_type', response.token_type);
          localStorage.setItem('isLoggedIn', 'true');
          
          // 提示登录成功
          Message.success('登录成功');
          
          // 显示加载中效果
          const loadingInstance = Loading.service({
            fullscreen: true,
            text: '正在进入系统...',
            spinner: 'el-icon-loading',
            background: 'rgba(255, 255, 255, 0.8)'
          });
          
          // 延迟跳转，让用户体验加载效果
          setTimeout(() => {
            // 关闭加载
            loadingInstance.close();
            // 跳转到主页
            this.$router.push('/');
          }, 1500); // 1.5秒后跳转，可以根据需要调整
        })
        .catch(error => {
          // 使用Element UI的Message组件显示错误
          if (error.message) {
            Message.error(error.message);
          }
        })
        .finally(() => {
          this.isLoading = false;
        });
      });
    }
  },
  // 如果已经登录，则直接跳转到首页
  created() {
    if (localStorage.getItem('isLoggedIn') === 'true') {
      this.$router.push('/');
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: 100vh;
  width: 100%;
}

/* 左侧样式 */
.login-left {
  width: 35%;
  background: linear-gradient(to bottom, #41a0e0, #2980c3);
  display: flex;
  flex-direction: column;
  padding: 30px;
  position: relative;
  overflow: hidden;
  box-shadow: 5px 0 15px rgba(0, 0, 0, 0.05);
}

.logo {
  display: flex;
  align-items: center;
}

.logo-square {
  width: 35px;
  height: 35px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  border-radius: 4px;
  color: #2980c3;
  font-weight: bold;
  font-size: 22px;
}

.logo-letter {
  transform: rotate(10deg);
}

.logo-text {
  color: white;
  font-size: 22px;
  font-weight: bold;
  letter-spacing: 1px;
}

.slogan {
  position: absolute;
  top: 50%;
  right: 30px;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.slogan-line {
  color: white;
  font-size: 36px;
  line-height: 1.4;
  font-weight: 500;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.slogan-space {
  height: 30px;
}

.cloud-decoration {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 150px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 600 200'%3E%3Cpath fill='%23f0f8ff' opacity='0.3' d='M0,180 C100,120 200,190 300,160 C400,130 500,180 600,150 L600,200 L0,200 Z'/%3E%3C/svg%3E");
  background-size: cover;
  background-position: center bottom;
}

/* 右侧样式 */
.login-right {
  width: 65%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  position: relative;
}

.login-form {
  width: 380px;
  margin-bottom: 100px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-weight: 400;
  font-size: 20px;
}

.forgot-password {
  text-align: right;
  margin-bottom: 20px;
}

.forgot-password a {
  color: #4889e4;
  text-decoration: none;
  font-size: 12px;
}

.login-btn {
  width: 100%;
  height: 40px;
}

.browser-notice {
  margin-top: 15px;
  padding: 8px 12px;
  background-color: #fff8f8;
  border-radius: 4px;
  border: 1px solid #ffebeb;
  display: flex;
  align-items: flex-start;
  font-size: 12px;
  color: #f56c6c;
  line-height: 1.5;
}

.browser-notice i {
  margin-right: 5px;
  font-size: 14px;
  margin-top: 1px;
  flex-shrink: 0;
}

.download-link {
  color: #4889e4;
  text-decoration: none;
  margin-left: 3px;
}

.login-footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 15px;
  text-align: center;
  color: #909399;
  font-size: 12px;
  background-color: #f5f7fa;
  border-top: 1px solid #eee;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    width: 100%;
    height: 200px;
  }
  
  .login-right {
    width: 100%;
  }
  
  .slogan {
    display: none;
  }
  
  .login-form {
    margin-top: 50px;
    margin-bottom: 150px;
  }
}
</style> 